import { axiosInstance } from 'src/boot/axios'
import CommonConfig from '../../assets/js/config'
import _ from 'lodash'
const reportUrl = CommonConfig.reportUrl + '/v1'
// !! 不良与停机报表很相似 所以写在一起
import qs from 'qs'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}
/**
  *列举停机原因日报表
  *
 **/
export function getStopReports ({ deviceIds, startDate, endDate, schemeLevel, schemeLevelId, dateType, pageSize, pageNum }) {
  let params = {
    deviceIds, // 设备id列表 待确认
    schemeLevel, // 组织层级
    schemeLevelId, // 组织层级ID,
    pageSize,
    pageNum
  }
  if (_.isNil(schemeLevel)) {
    return
  }
  switch (dateType) {
    case 'date':
      params = { ...params, startDate, endDate }
      break
    case 'week':
      params = { ...params, startWeek: startDate, endWeek: endDate }
      break
    case 'month':
      params = { ...params, startMonth: startDate, endMonth: endDate }
  }
  const newUrlList =
  {
    // 列举停机原因日报表
    date: `/halting_cause_daily_reports:page`,
    // 列举停机原因周报表
    week: `/halting_cause_weekly_reports:page`,
    // 列举停机原因月报表
    month: `/halting_cause_monthly_reports:page`
  }
  const newUrl = reportUrl + newUrlList[dateType]
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

export function getStopStatistics ({ deviceIds, startDate, endDate, schemeLevel, schemeLevelId, dateType, pageSize, pageNum }) {
  let params = {
    deviceIds, // 设备id列表 待确认
    schemeLevel, // 组织层级
    schemeLevelId, // 组织层级ID,
    pageSize,
    pageNum
  }
  if (_.isNil(schemeLevel)) {
    return
  }
  switch (dateType) {
    case 'date':
      params = { ...params, startDate, endDate }
      break
    case 'week':
      params = { ...params, startWeek: startDate, endWeek: endDate }
      break
    case 'month':
      params = { ...params, startMonth: startDate, endMonth: endDate }
  }
  const newUrlList =
    {
      // 列举停机原因日报表
      date: `/halting_cause_daily_reports/statistics`,
      // 列举停机原因周报表
      week: `/halting_cause_weekly_reports/statistics`,
      // 列举停机原因月报表
      month: `/halting_cause_monthly_reports/statistics`
    }
  const newUrl = reportUrl + newUrlList[dateType]
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

export function downloadStopExcel ({ deviceIds, startDate, endDate, schemeLevel, schemeLevelId, dateType, requestId, workshopName }) {
  let params = {
    deviceIds, // 设备id列表 待确认
    schemeLevel, // 组织层级
    schemeLevelId, // 组织层级ID,
    requestId,
    workshopName
  }
  if (_.isNil(schemeLevel)) {
    return
  }
  switch (dateType) {
    case 'date':
      params = { ...params, startDate, endDate }
      break
    case 'week':
      params = { ...params, startWeek: startDate, endWeek: endDate }
      break
    case 'month':
      params = { ...params, startMonth: startDate, endMonth: endDate }
  }
  const newUrlList =
    {
      // 列举停机原因日报表
      date: `/halting_cause_daily_reports/excel:export`,
      // 列举停机原因周报表
      week: `/halting_cause_weekly_reports/excel:export`,
      // 列举停机原因月报表
      month: `/halting_cause_monthly_reports/excel:export`
    }
  const newUrl = reportUrl + newUrlList[dateType]
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 列举停机原因报表弹窗明细
 * halting_cause_daily_report_items
 */
export function getDailyReport ({
  schemeLevel,
  schemeLevelId,
  startDate,
  causeId,
  deviceIds,
  endDate,
  causeGroupId,
  dateType
}) {
  let reqData = {
    params: {
      schemeLevel,
      schemeLevelId,
      causeId,
      causeGroupId,
      deviceIds
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  switch (dateType) {
    case 'date':
      reqData.params = { ...reqData.params, startDate, endDate }
      break
    case 'week':
      reqData.params = { ...reqData.params, startWeek: startDate, endWeek: endDate }
      break
    case 'month':
      reqData.params = { ...reqData.params, startMonth: startDate, endMonth: endDate }
  }
  const newUrlList =
  {
    // 列举停机原因日报表
    date: `/halting_cause_daily_report_items`,
    // 列举停机原因周报表
    week: `/halting_cause_weekly_report_items`,
    // 列举停机原因月报表
    month: `/halting_cause_monthly_report_items`
  }
  const newUrl = reportUrl + newUrlList[dateType]
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// ================================不良数量报表========================================
/**
  *列举不良原因日报表,周报表,月报表
  *
 **/
export function getBadReports ({ deviceIds, startDate, endDate, schemeLevel, productIds, schemeLevelId, dateType, causeType }) {
  const reqData = {
    params: {
      deviceIds, // 设备id列表 待确认
      schemeLevel, // 组织层级
      schemeLevelId, // 组织层级ID
      type: causeType,
      productIds // 产品ID
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  if (_.isNil(schemeLevel)) {
    return
  }
  switch (dateType) {
    case 'date':
      reqData.params = { ...reqData.params, startDate, endDate }
      break
    case 'week':
      reqData.params = { ...reqData.params, startWeek: startDate, endWeek: endDate }
      break
    case 'month':
      reqData.params = { ...reqData.params, startMonth: startDate, endMonth: endDate }
  }
  const newUrlList =
  {
    // 列举停机原因日报表
    date: `/bad_cause_daily_reports`,
    // 列举停机原因周报表
    week: `/bad_cause_week_reports`,
    // 列举停机原因月报表
    month: `/bad_cause_month_reports`
  }
  const newUrl = reportUrl + newUrlList[dateType]
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 * 列举不良原因报表弹窗明细
 *
 */
export function getDailyBadReport ({
  schemeLevel,
  schemeLevelId,
  startDate,
  causeId,
  productIds,
  deviceIds,
  endDate,
  causeGroupId,
  dateType,
  type
}) {
  let reqData = {
    params: {
      schemeLevel,
      schemeLevelId,
      causeId,
      productIds,
      causeGroupId,
      deviceIds,
      type
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  switch (dateType) {
    case 'date':
      reqData.params = { ...reqData.params, startDate, endDate }
      break
    case 'week':
      reqData.params = { ...reqData.params, startWeek: startDate, endWeek: endDate }
      break
    case 'month':
      reqData.params = { ...reqData.params, startMonth: startDate, endMonth: endDate }
  }
  const newUrlList =
  {
    // 列举停机原因日报表
    date: `/bad_cause_daily_reports_detail`,
    // 列举停机原因周报表
    week: `/bad_cause_week_reports_detail`,
    // 列举停机原因月报表
    month: `/bad_cause_month_reports_detail`
  }
  const newUrl = reportUrl + newUrlList[dateType]
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
/**
 * 列举不良原因报表合格率弹窗明细
 *
 */
export function getBadReportDetail ({
  schemeLevel,
  schemeLevelId,
  startDate,
  causeId,
  deviceIds,
  endDate,
  causeGroupId,
  dateType,
  productIds,
  type
}) {
  let reqData = {
    params: {
      schemeLevel,
      schemeLevelId,
      causeId,
      causeGroupId,
      productIds,
      deviceIds,
      type
    },
    paramsSerializer: function (params) {
      return qs.stringify(params, qsConfig)
    }
  }
  switch (dateType) {
    case 'date':
      reqData.params = { ...reqData.params, startDate, endDate }
      break
    case 'week':
      reqData.params = { ...reqData.params, startWeek: startDate, endWeek: endDate }
      break
    case 'month':
      reqData.params = { ...reqData.params, startMonth: startDate, endMonth: endDate }
  }
  const newUrlList =
  {
    // 列举不良原因日报表合格率
    date: `/bad_cause_daily_reports_rate_detail`,
    // 列举不良原因周报表合格率
    week: `/bad_cause_week_reports_rate_detail`,
    // 列举不良原因月报表合格率
    month: `/bad_cause_month_reports_rate_detail`
  }
  const newUrl = reportUrl + newUrlList[dateType]
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 不良原因报表导出
export function exportBadCauseReports (params) {
  const newUrl = `${reportUrl}/bad_cause_daily_reports/excel:export`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}
